package com.sxkiler.demo.test.common;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApiInfo {
    String method;
    String uri;
    String contentType;

    Map<String,String> headers = new HashMap();
    Map<String,String> cookies = new HashMap();
    Map<String,String> urlParam = new HashMap();
    Map<String,Object> bodyParam = new HashMap<>();
    HttpRequest request;

    private ApiInfo(){}
    public static ApiInfo build(String uri,String method){
        return new ApiInfo().uri(uri).method(method);
    }
    public  ApiInfo contentType(String contentType){
        this.contentType = contentType;
        return this;
    }
    public  ApiInfo method(String method){
        this.method = method;
        return this;
    }
    public  ApiInfo uri(String uri){
        this.uri = uri;
        return this;
    }
    public  ApiInfo urlParam(String key,String value){
        this.urlParam.put(key,value);
        return this;
    }
    public  ApiInfo bodyParam(String key,Object value){
        this.bodyParam.put(key,value);
        return this;
    }
    public ApiInfo bodyParam(Map paramMap){
        this.bodyParam.putAll(paramMap);
        return this;
    }
    public  ApiInfo addHeader(String key,String value){
        this.headers.put(key,value);
        return this;
    }
    public  ApiInfo addCookie(String key,String value){
        this.cookies.put(key,value);
        return this;
    }

    public HttpRequest getRequest(){
        if(request==null && StrUtil.isNotEmpty(uri) && StrUtil.isNotEmpty(method)){
            if("post".equalsIgnoreCase(this.method)) {
                return HttpUtil.createPost(uri);
            }else{
                return HttpUtil.createGet(uri);
            }
        }
        return null;
    }

    /**
     * 补全url，同时智能补全/
     * @return
     */
    private String getUrl(){
        StringBuffer urlBuf = new StringBuffer(this.uri);
        if(ObjectUtil.isNotEmpty(urlParam)){
            urlParam.forEach((name,value)->{
                if(urlBuf.indexOf("?")<0){
                    urlBuf.append("?");
                }else{
                    urlBuf.append("&");
                }
                urlBuf.append(name).append("=").append(URLUtil.encode(value));
            });
        }
        return urlBuf.toString();
    }
    public String sendByHttpClient4() throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(getUrl());
        CloseableHttpResponse response = client.execute(post);
        return IoUtil.read(response.getEntity().getContent(),"Utf-8");
    }
    public HttpResponse sendByHutool(){
        return sendByHutool(60000);
    }
    public HttpResponse sendByHutool(int timeout){
        this.uri = getUrl();
        System.out.println(this.uri);
        HttpRequest request = getRequest();
        if("post".equalsIgnoreCase(this.method)) {
            if (ObjectUtil.isNotEmpty(bodyParam)){
                if (StrUtil.isNotEmpty(contentType) && contentType.indexOf("json") >= 0) {
                    String json = JSON.toJSONString(bodyParam);
                    request.body(json.getBytes());
                    request.contentType(this.contentType);
                } else {
                    request.form(bodyParam);
                }
            }
        }
        request.timeout(timeout);
        request.addHeaders(headers);
        List<HttpCookie> cookieList = new ArrayList<>();
        this.cookies.forEach((name,value)->{
            cookieList.add(new HttpCookie(name,value));
        });
        if(ObjectUtil.isNotEmpty(cookieList)){
            request.cookie(cookieList.toArray(new HttpCookie[]{}));
        }
        return request.execute();
    }
}
